﻿var modeType = null;

$(document).ready(function () {
    $("#btnBack").click(handleBack);

    $(".checkbox").change(handleSelectChkDel);

    $("#btnDelete").click(handleDelete);

    $("#btnAddGroup").click(function () {
        modeType = NEW_GROUP;
        showAddGroupPage();
    });

    $("#btnAddSubGroup").click(function () {
        modeType = NEW_SUB_GROUP;
        showAddGroupPage();
    });

    $("#cboGroup").change(function () {
        var groupId = $(this).val();
        getSubGroup(groupId);

        if (groupId == "0") {
            $("#btnAddSubGroup").addClass("disabled");
            $("#btnAddSubGroup").attr("disabled", "disabled");
        }
        else {
            $("#btnAddSubGroup").removeClass("disabled");
            $("#btnAddSubGroup").removeAttr("disabled");
        }
    });

    registerUpdateEvent();
});

var handleUpdate = function (id) {
    var action = "/Master/ShowAddGroupPage";
    var param = "?parentGroup=" + $("#cboGroup").val() + "&modeType=" + modeType + "&subId=" + id;
    $("#popup").attr("src", action + param);
    showPopup();
    return false;
};

var showAddGroupPage = function (event) {
    var action = "/Master/ShowAddGroupPage";
    var param = "?parentGroup=" + $("#cboGroup").val() + "&modeType=" + modeType;
    $("#popup").attr("src", action + param);
    showPopup();
    return false;
}

function getSubGroup(id) {
    $.ajax({
        type: "POST",
        url: "/Master/GetSubGroup",
        async: false,
        data: {
            itemGroupId: id
        },
        dataType: "json",
        success: function (response) {
            if (response == null || response == ERROR_PAGE) {
                alert("Something wrong was happened");
            }
            else if (response == LOST_SESSION) {
                redirectToLogin();
            }
            else {
                loadComboSubGroup(response);
            }
        }
    });
}

var handleDelete = function (e) {
    var arrDel = [];

    $(".checkbox:checked").each(function () {
        arrDel.push($(this).attr("subid"));
    });

    $.ajax({
        type: "POST",
        url: "/Master/DeleteSubGroup",
        async: false,
        data: {
            arrDel: JSON.stringify(arrDel)
        },
        dataType: "json",
        success: function (response) {
            if (response == null || response == ERROR_PAGE) {
                alert("Something wrong was happened");
            }
            else if (response == LOST_SESSION) {
                redirectToLogin();
            }
            else {
                $(".checkbox:checked").each(function () {
                    $(this).parent().parent().remove();
                });

                handleSelectChkDel();
            }
        }
    });
};

var handleSelectChkDel = function (e) {
    if ($(".checkbox:checked").length > 0) {
        $("#btnDelete").removeAttr("disabled");
        $("#btnDelete").removeClass("disabled");
    }
    else {
        $("#btnDelete").attr("disabled", "disabled");
        $("#btnDelete").addClass("disabled");
    }
};

function loadComboSubGroup(data) {
    $(".info table tbody").empty();
    handleSelectChkDel();

    if (data == null || data == "") {
        return;
    }

    for (var i = 0; i < data.length; i++) {
        var tr = $("<tr></tr>");
        var chkTd = $('<td class="width-20"><input type="checkbox" name="chkDel" class="checkbox" subId="' +
            data[i].id + '" groupId="' + data[i].groupId + '" /></td>');
        var nameTd = $('<td><a href="#" class="groupname" id="' + data[i].id + '">' + data[i].SubGroupName + '</a></td>');
        $(chkTd).find(".checkbox").change(handleSelectChkDel);
        $(tr).append(chkTd);
        $(tr).append(nameTd);
        $(".info table tbody").append(tr);
    }

    registerUpdateEvent();
}

var handleBack = function (event) {
    var controler = "Master";
    var action = "Main";
    var goBack = $("#goBack").val();
    var arr = goBack.split(";");
    var groupId = "";
    var subGroupId = "";

    if (goBack.length >= 2) {
        groupId = arr[0];
        subGroupId = arr[1];
    }

    var param = "pageIndex=1&groupId=" + groupId + "&subGroupId=" + subGroupId;

    redirectToPage(controler, action, param);
}

function refreshPage(id, groupName, mode) {
    if (mode == NEW_GROUP) {
        var selected = $("#cboGroup").val();
        $("#cboGroup").prepend("<option value='" + id + "'>" + groupName + "</option>");
        $("#cboGroup option[value='0']").remove();
        $("#cboGroup").prepend("<option value='0'>Chọn nhóm cha</option>");
        $("#cboGroup").val(selected);
    }
    else if (mode == NEW_SUB_GROUP) {
        var tr = $('<tr>' +
                    '<td class="width-20">' +
                        '<input type="checkbox" name="chkDel" class="checkbox" subid="' + id + '" groupid="undefined">' +
                    '</td>' +
                    '<td><a href="#" class="groupname" id="' + id + '">' + groupName + '<a/></td>' +
                '</tr>');
        $(".info tbody").prepend(tr);

        registerUpdateEvent();
    }
    else if (mode == UPDATE_SUB_GROUP) {
        $("a#" + id + ".groupname").text(groupName);
    }
}

function registerUpdateEvent() {
    $(".groupname").click(function () {
        modeType = UPDATE_SUB_GROUP;
        var id = $(this).attr("id");
        handleUpdate(id);
    });
}